clear all

********************************************************************************
// ZCTA level data
********************************************************************************

use "ZCTALevelData_R2.dta", clear
keep if year==2015

ren state statea 
ren county countya
tostring statea, gen(stated)
tostring countya, gen(countyd)
gen z = "0"
gen c_l = length(countyd)
gen s_l = length(stated)
gen state = ""
replace state = stated if s_l==2
egen state_input = concat(z stated), p("")
replace state = state_input if s_l==1
gen county2 = ""
replace county2 = countyd if c_l==3
egen county_input1 = concat(z countyd), p("")
egen county_input2 = concat(z z countyd), p("")
replace county2 = county_input1 if c_l==2
replace county2 = county_input2 if c_l==1
egen county = concat(state county2), p("")
drop county2 county_input* state_inpu c_l s_l z countyd stated
drop if state=="02"|state=="15"|state=="72"|state=="78"|state=="60"|state=="66"|state=="69"
drop if tot_pop==.
drop if tot_pop==0
drop if cms_wage_index==.

gen cat_ucc = n_urgentcare
replace cat_ucc = 4 if cat_ucc>=4
gen cat_hospaff_ucc = n_hospaffucc
replace cat_hospaff_ucc = 2 if cat_hospaff_ucc>=2
gen cat_hospaff_ucc_geo = n_hospaffucc_geo
replace cat_hospaff_ucc_geo = 2 if cat_hospaff_ucc_geo>=2
gen cat_hospaff_ucc_geo2 = n_hospaffucc_geo2
replace cat_hospaff_ucc_geo2 = 2 if cat_hospaff_ucc_geo2>=2

egen mean_inc = mean(income_pc), by(county)
replace income_pc = mean_inc if income_pc==.
drop mean_inc
replace income_pc = income_pc/10000
replace median_value = median_value/1000000
replace median_gross_rent = median_gross_rent/1000
gen any_hosp = (n_hospitals>0)
gen immigration = moved_within_county+moved_within_state+moved_state+moved_abroad
replace immigration = 0 if immigration==.
gen gte_highschool = highschool + some_college + bachelor_higher
replace gte_highschool = 0 if gte_highschool==.

local demog "nonhisp_white nonhisp_black nonhisp_other hispanic gte_highschool female age_65 uninsured medicare medicaid"
foreach dem of local demog{
	replace `dem' = `dem'/100
}

gen median_hisp = .
qui sum hispanic, detail
replace median_hisp = r(p50)
gen above_median_hisp = (hispanic>=median_hisp)
drop median_hisp

gen median_black = .
qui sum nonhisp_black, detail
replace median_black = r(p50)
gen above_median_black = (nonhisp_black>=median_black)
drop median_black

gen median_income_pc = .
qui sum income_pc, detail
replace median_income_pc = r(p50)
gen high_income = (income_pc>=median_income_pc)
drop median_income_pc

gen median_svi = .
qui sum rpl_themes if year==2015, detail
replace median_svi = r(p50)
gen high_svi = (rpl_themes>=median_svi)
drop median_svi
gen intercept = 1

gen median_black = .
qui sum nonhisp_black if high_svi==1, detail
replace median_black = r(p50)
gen abv_med_blck_high_svi = (nonhisp_black>=median_black) if high_svi==1
drop median_black

gen median_income_pc = .
qui sum income_pc if high_svi==1, detail
replace median_income_pc = r(p50)
gen high_income_high_svi = (income_pc>=median_income_pc) if high_svi==1
drop median_income_pc

gen median_uninsured = .
qui sum uninsured, detail
replace median_uninsured = r(p50)
gen high_uninsured = (uninsured>=median_uninsured)
drop median_uninsured

// Instrument: CON laws as of 2019

// Blue states in ncsl.org

gen con_laws = .
replace con_laws = 1 if state=="02"| state=="15"| state=="53"| state=="41"| state=="30"| state=="32"| state=="31"| state=="19"| state=="40"| state=="29"| state=="17"| state=="22"| state=="05"| state=="21"| state=="18"| state=="26"| state=="39"| state=="54"| state=="47"| state=="28"| state=="01"| state=="51"| state=="24"| state=="36"| state=="50"| state=="23"| state=="25"| state=="44"| state=="34"| state=="09"| state=="10"| state=="37"| state=="45"| state=="13"| state=="12"
replace con_laws = 0 if con_laws==.

// Blue and green states in ncsl.org

gen con_laws2 = con_laws
replace con_laws2 = 1 if state=="55"|state=="27"|state=="04"

// Community catalyst report

gen ucc_regulation = .
replace ucc_regulation = 1 if state=="04"| state=="09"| state=="10"| state=="12"| state=="04"| state=="33"| state=="34"| state=="35"| state=="36"| state=="38"| state=="41"| state=="44"| state=="49"
replace ucc_regulation = 0 if ucc_regulation==.

gen con_laws3 = con_laws 
replace con_laws3 = 0 if ucc_regulation==1

// CON intensity The Effect of Certificate of Need Laws on All-Cause Mortality, Health Services Research, 2018, 53(1)

gen con_intensity = .
replace con_intensity = 18 if state=="01"
replace con_intensity = 18 if state=="02"
replace con_intensity = 0 if state=="04"
replace con_intensity = 4 if state=="05"
replace con_intensity = 0 if state=="06"
replace con_intensity = 0 if state=="08"
replace con_intensity = 15 if state=="09"
replace con_intensity = 7 if state=="10"
replace con_intensity = 26 if state==""
replace con_intensity = 9 if state=="12"
replace con_intensity = 16 if state=="13"
replace con_intensity = 25 if state=="15"
replace con_intensity = 0 if state=="16"
replace con_intensity = 14 if state=="17"
replace con_intensity = 0 if state=="18"
replace con_intensity = 8 if state=="19"
replace con_intensity = 0 if state=="20"
replace con_intensity = 16 if state=="21"
replace con_intensity = 2 if state=="22"
replace con_intensity = 23 if state=="23"
replace con_intensity = 14 if state=="24"
replace con_intensity = 13 if state=="25"
replace con_intensity = 17 if state=="26"
replace con_intensity = 0 if state=="27"
replace con_intensity = 16 if state=="28"
replace con_intensity = 13 if state=="29"
replace con_intensity = 6 if state=="30"
replace con_intensity = 1 if state=="31"
replace con_intensity = 3 if state=="32"
replace con_intensity = 12 if state=="33"
replace con_intensity = 11 if state=="34"
replace con_intensity = 0 if state=="35"
replace con_intensity = 17 if state=="36"
replace con_intensity = 22 if state=="37"
replace con_intensity = 0 if state=="38"
replace con_intensity = 0 if state=="39"
replace con_intensity = 3 if state=="40"
replace con_intensity = 2 if state=="41"
replace con_intensity = 0 if state=="42"
replace con_intensity = 19 if state=="44"
replace con_intensity = 18 if state=="45"
replace con_intensity = 0 if state=="46"
replace con_intensity = 18 if state=="47"
replace con_intensity = 0 if state=="48"
replace con_intensity = 0 if state=="49"
replace con_intensity = 28 if state=="50"
replace con_intensity = 18 if state=="51"
replace con_intensity = 15 if state=="53"
replace con_intensity = 19 if state=="54"
replace con_intensity = 2 if state=="55"
replace con_intensity = 0 if state=="56"
replace con_intensity = 0 if con_intensity==.

gen con_facilities = 0
replace con_facilities = 10 if state=="02"
replace con_facilities = 8 if state=="53"
replace con_facilities = 3 if state=="41"
replace con_facilities = 3 if state=="30"
replace con_facilities = 4 if state=="32"
replace con_facilities = 9 if state=="19"
replace con_facilities = 4 if state=="31"
replace con_facilities = 6 if state=="40"
replace con_facilities = 7 if state=="17"
replace con_facilities = 6 if state=="29"
replace con_facilities = 8 if state=="22"
replace con_facilities = 5 if state=="26"
replace con_facilities = 1 if state=="18"
replace con_facilities = 19 if state=="21"
replace con_facilities = 4 if state=="05"
replace con_facilities = 13 if state=="28"
replace con_facilities = 1 if state=="39"
replace con_facilities = 1 if state=="54"
replace con_facilities = 11 if state=="47"
replace con_facilities = 18 if state=="01"
replace con_facilities = 10 if state=="13"
replace con_facilities = 10 if state=="12"
replace con_facilities = 8 if state=="24"
replace con_facilities = 11 if state=="51"
replace con_facilities = 11 if state=="45"
replace con_facilities = 9 if state=="23"
replace con_facilities = 17 if state=="50"
replace con_facilities = 10 if state=="36"
replace con_facilities = 6 if state=="25"
replace con_facilities = 9 if state=="44"
replace con_facilities = 21 if state=="34"
replace con_facilities = 13 if state=="09"
replace con_facilities = 5 if state=="10"
replace con_facilities = 12 if state=="37"
replace con_facilities = 11 if state=="45"

replace tot_pop = tot_pop/10000
replace tot_pop = tot_pop/1000
gen tot_pop2 = tot_pop
gen tot_pop3 = tot_pop

gen cat_hosp = n_hospitals
replace cat_hosp = 1 if cat_hosp>1
replace con_intensity = con_intensity/28

saveold "ZCTALevelProcessed_R2_input.dta", version(12) replace

********************************************************************************
// HSA level data
********************************************************************************

use "HSALevelData_R2.dta", clear
ren state statea 
ren county countya
tostring statea, gen(stated)
tostring countya, gen(countyd)
gen z = "0"
gen c_l = length(countyd)
gen s_l = length(stated)
gen state = ""
replace state = stated if s_l==2
egen state_input = concat(z stated), p("")
replace state = state_input if s_l==1
gen county2 = ""
replace county2 = countyd if c_l==3
egen county_input1 = concat(z countyd), p("")
egen county_input2 = concat(z z countyd), p("")
replace county2 = county_input1 if c_l==2
replace county2 = county_input2 if c_l==1
egen county = concat(state county2), p("")
drop county2 county_input* state_inpu c_l s_l z countyd stated

drop if state=="02"|state=="15"|state=="72"|state=="78"|state=="60"|state=="66"|state=="69"

drop if tot_pop==.
drop if tot_pop==0
drop if cms_wage_index==.

sum tot_pop, detail
gen cat_ucc = n_urgentcare
replace cat_ucc = 4 if cat_ucc>=4
gen cat_hospaff_ucc = n_hospaffucc
replace cat_hospaff_ucc = 2 if cat_hospaff_ucc>=2
gen cat_hospaff_ucc_geo = n_hospaffucc_geo
replace cat_hospaff_ucc_geo = 2 if cat_hospaff_ucc_geo>=2
gen cat_hospaff_ucc_geo2 = n_hospaffucc_geo2
replace cat_hospaff_ucc_geo2 = 2 if cat_hospaff_ucc_geo2>=2

replace income_pc = income_pc/10000
gen any_hosp = (n_hospitals>0)

local demog "nonhisp_white nonhisp_black nonhisp_other hispanic gte_highschool female age_65 uninsured  medicare medicaid"
foreach dem of local demog{
	replace `dem' = `dem'/100
}

gen median_hisp = .
qui sum hispanic, detail
replace median_hisp = r(p50)
gen above_median_hisp = (hispanic>=median_hisp)
drop median_hisp

gen median_black = .
qui sum nonhisp_black, detail
replace median_black = r(p50)
gen above_median_black = (nonhisp_black>=median_black)
drop median_black

gen median_income_pc = .
qui sum income_pc, detail
replace median_income_pc = r(p50)
gen high_income = (income_pc>=median_income_pc)
drop median_income_pc

gen median_svi = .
qui sum rpl_themes, detail
replace median_svi = r(p50)
gen high_svi = (rpl_themes>=median_svi)
drop median_svi
gen intercept = 1

gen median_black = .
qui sum nonhisp_black if high_svi==1, detail
replace median_black = r(p50)
gen abv_med_blck_high_svi = (nonhisp_black>=median_black) if high_svi==1
drop median_black

gen median_income_pc = .
qui sum income_pc if high_svi==1, detail
replace median_income_pc = r(p50)
gen high_income_high_svi = (income_pc>=median_income_pc) if high_svi==1
drop median_income_pc

// Instrument CON laws

gen con_laws = .
replace con_laws = 1 if state=="02"| state=="15"| state=="53"| state=="41"| state=="30"| state=="32"| state=="31"| state=="19"| state=="40"| state=="29"| state=="17"| state=="22"| state=="05"| state=="21"| state=="18"| state=="26"| state=="39"| state=="54"| state=="47"| state=="28"| state=="01"| state=="51"| state=="24"| state=="36"| state=="50"| state=="23"| state=="25"| state=="44"| state=="34"| state=="09"| state=="10"| state=="37"| state=="45"| state=="13"| state=="12"
replace con_laws = 0 if con_laws==.

gen con_laws2 = con_laws
replace con_laws2 = 1 if state=="55"|state=="27"|state=="04"

gen ucc_regulation = .
replace ucc_regulation = 1 if state=="04"| state=="09"| state=="10"| state=="12"| state=="04"| state=="33"| state=="34"| state=="35"| state=="36"| state=="38"| state=="41"| state=="44"| state=="49"
replace ucc_regulation = 0 if ucc_regulation==.

gen con_laws3 = con_laws 
replace con_laws3 = 0 if ucc_regulation==1

gen con_intensity = .
replace con_intensity = 18 if state=="01"
replace con_intensity = 18 if state=="02"
replace con_intensity = 0 if state=="04"
replace con_intensity = 4 if state=="05"
replace con_intensity = 0 if state=="06"
replace con_intensity = 0 if state=="08"
replace con_intensity = 15 if state=="09"
replace con_intensity = 7 if state=="10"
replace con_intensity = 26 if state==""
replace con_intensity = 9 if state=="12"
replace con_intensity = 16 if state=="13"
replace con_intensity = 25 if state=="15"
replace con_intensity = 0 if state=="16"
replace con_intensity = 14 if state=="17"
replace con_intensity = 0 if state=="18"
replace con_intensity = 8 if state=="19"
replace con_intensity = 0 if state=="20"
replace con_intensity = 16 if state=="21"
replace con_intensity = 2 if state=="22"
replace con_intensity = 23 if state=="23"
replace con_intensity = 14 if state=="24"
replace con_intensity = 13 if state=="25"
replace con_intensity = 17 if state=="26"
replace con_intensity = 0 if state=="27"
replace con_intensity = 16 if state=="28"
replace con_intensity = 13 if state=="29"
replace con_intensity = 6 if state=="30"
replace con_intensity = 1 if state=="31"
replace con_intensity = 3 if state=="32"
replace con_intensity = 12 if state=="33"
replace con_intensity = 11 if state=="34"
replace con_intensity = 0 if state=="35"
replace con_intensity = 17 if state=="36"
replace con_intensity = 22 if state=="37"
replace con_intensity = 0 if state=="38"
replace con_intensity = 0 if state=="39"
replace con_intensity = 3 if state=="40"
replace con_intensity = 2 if state=="41"
replace con_intensity = 0 if state=="42"
replace con_intensity = 19 if state=="44"
replace con_intensity = 18 if state=="45"
replace con_intensity = 0 if state=="46"
replace con_intensity = 18 if state=="47"
replace con_intensity = 0 if state=="48"
replace con_intensity = 0 if state=="49"
replace con_intensity = 28 if state=="50"
replace con_intensity = 18 if state=="51"
replace con_intensity = 15 if state=="53"
replace con_intensity = 19 if state=="54"
replace con_intensity = 2 if state=="55"
replace con_intensity = 0 if state=="56"
replace con_intensity = 0 if con_intensity==.

gen con_facilities = 0
replace con_facilities = 10 if state=="02"
replace con_facilities = 8 if state=="53"
replace con_facilities = 3 if state=="41"
replace con_facilities = 3 if state=="30"
replace con_facilities = 4 if state=="32"
replace con_facilities = 9 if state=="19"
replace con_facilities = 4 if state=="31"
replace con_facilities = 6 if state=="40"
replace con_facilities = 7 if state=="17"
replace con_facilities = 6 if state=="29"
replace con_facilities = 8 if state=="22"
replace con_facilities = 5 if state=="26"
replace con_facilities = 1 if state=="18"
replace con_facilities = 19 if state=="21"
replace con_facilities = 4 if state=="05"
replace con_facilities = 13 if state=="28"
replace con_facilities = 1 if state=="39"
replace con_facilities = 1 if state=="54"
replace con_facilities = 11 if state=="47"
replace con_facilities = 18 if state=="01"
replace con_facilities = 10 if state=="13"
replace con_facilities = 10 if state=="12"
replace con_facilities = 8 if state=="24"
replace con_facilities = 11 if state=="51"
replace con_facilities = 11 if state=="45"
replace con_facilities = 9 if state=="23"
replace con_facilities = 17 if state=="50"
replace con_facilities = 10 if state=="36"
replace con_facilities = 6 if state=="25"
replace con_facilities = 9 if state=="44"
replace con_facilities = 21 if state=="34"
replace con_facilities = 13 if state=="09"
replace con_facilities = 5 if state=="10"
replace con_facilities = 12 if state=="37"
replace con_facilities = 11 if state=="45"

replace tot_pop = tot_pop/10000
replace tot_pop = tot_pop/1000
gen tot_pop2 = tot_pop
gen tot_pop3 = tot_pop

gen cat_hosp = n_hospitals
replace cat_hosp = 1 if cat_hosp>1
replace con_intensity = con_intensity/28

saveold "HSALevelDataProcessed_R2.dta", version(12) replace
